//
//  DYSegementViewController.m
//  Lazy
//
//  Created by 帝云科技 on 2018/7/24.
//  Copyright © 2018年 帝云科技. All rights reserved.
//

#import "DYSegementViewController.h"

#define kSegmentHeight 43

@interface DYSegementViewController ()<
UICollectionViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout>

@end

@implementation DYSegementViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)dy_initData {
    [super dy_initData];
}

- (void)dy_initUI {
    [super dy_initUI];
    [self.view addSubview:self.segmentView];
    [self.view addSubview:self.collectionView];
}

#pragma mark -
- (void)segmentViewSelectedIndex:(NSInteger)selectedIndex {
    CGPoint point = CGPointMake(_collectionView.xhq_width * selectedIndex, 0);
    [_collectionView setContentOffset:point animated:self.animatedOfScrollToOffset];
    self.segmentView.selectedSegmentIndex=selectedIndex;
}
-(void)change:(UISegmentedControl *)sege
{
    NSInteger seletedIndex=sege.selectedSegmentIndex;
    [self segmentViewSelectedIndex:seletedIndex];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger seletedIndex = (NSInteger)scrollView.contentOffset.x / scrollView.xhq_width;
    [self segmentViewSelectedIndex:seletedIndex];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.childViewControllers.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class]) forIndexPath:indexPath];
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    UIViewController *vc = self.childViewControllers[indexPath.row];
    [cell.contentView addSubview:vc.view];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *vc = self.childViewControllers[indexPath.row];
    [vc viewWillAppear:YES];
}

#pragma mark - getter
- (UISegmentedControl *)segmentView {
    if (!_segmentView) {
         _segmentView=[[UISegmentedControl alloc]initWithItems:self.titleArray];
        _segmentView.frame=CGRectMake(BILIWIDTH(10),self.segmentHeight/2-BILIHEIGHT(15), kScreenWidth-BILIWIDTH(20), BILIHEIGHT(30));
        _segmentView.tintColor=[UIColor xhq_base];
        _segmentView.selectedSegmentIndex=self.currentIndex;
        [_segmentView addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
        NSDictionary *attributes = [NSDictionary dictionaryWithObject:kFont(15)
                                                               forKey:NSFontAttributeName];
        [_segmentView setTitleTextAttributes:attributes
                                   forState:UIControlStateNormal];
    }
    return _segmentView;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.itemSize = CGSizeMake(kScreenWidth, kScreenHeight - self.segmentHeight - kNavigationStatusHeight);
        
        _collectionView = [[DYPageCollectionView alloc]initWithFrame:CGRectMake(0, kSegmentHeight, kScreenWidth, kScreenHeight - kNavigationStatusHeight - self.segmentHeight) collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.bounces = NO;
        _collectionView.scrollEnabled = YES;
        _collectionView.pagingEnabled = YES;
        _collectionView.showsHorizontalScrollIndicator = NO;
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class])];
        if (_currentIndex > 0) {
            [self segmentViewSelectedIndex:self.currentIndex];
        }
    }
    return _collectionView;
}

- (NSArray *)titleArray {
    if (!_titleArray) {
        _titleArray = [[NSArray alloc]init];
    }
    return _titleArray;
}

- (CGFloat)segmentHeight {
    if (!_segmentHeight) {
        _segmentHeight = kSegmentHeight;
    }
    return _segmentHeight;
}

- (NSInteger)currentIndex {
    if (!_currentIndex) {
        _currentIndex = 0;
    }
    return _currentIndex;
}

@end
